<script>
import base from "../actionBase";
import Selector from "@/components/selector";
import $ from "@/common";
export default {
  name: "action_caseDistribute",
  extends: base,
  data() {
    return {
      loading: false
    };
  },
  computed: {
  },
  methods: {
    actionTrigger() {
      return new Promise((resolve, reject) => {
        this.$Modal.confirm({
          title: "确认操作",
          content: this.confirmText(),
          onOk: () =>
            this.actionSubmit()
              .then(res => {
                resolve(res);
              })
              .catch(res => {
                reject(res);
              }),
          onCancel() {
            resolve();
          }
        });
      });
    },
    confirmText() {
      var count = this.$owner.totalCount;
      var text = "";
      var rows = this.$owner.getSelectedIds();
      if (rows && rows.length) {
        count = rows.length;
        text = "确认导出当前选中的" + count + "条数据";
      } else {
        if (count) {
          text = "确认导出当前过滤的" + count + "条数据";
        } else {
          text = "确认导出当前过滤的数据";
        }
      }
      return text;
    },
    actionSubmit() {
      var data = {};
      var rows = this.$owner.getSelectedIds();

      data.param = {};
      if (!this.$owner.filterKey || !$.trim(this.$owner.filterKey).length) {
        $.setObj(data.param, this.$owner.filterObj);
      } else {
        data.param.filter = this.$owner.filterKey;
      }
      if (rows && rows.length) {
        data.rows = rows;
      }
      this.loading = true;
      return $.download(this.actionModel.path, data)
        .then(res => {
          this.actionSuccess();
        })
        .finally(() => {
          this.loading = false;
        });
    }
  },
  created() {
    if (!this.$owner.listDgata && this.$owner.$parent.listData) {
      this.$owner = this.$owner.$parent;
    }
  },
  components: {
    Selector
  }
};
</script>

<style lang="less">
.action-distribute {
  margin-right: 8px;
  .select-box {
    display: inline-block;
  }
  .ivu-select-selection {
    border-radius: 4px 0 0 4px;
  }
  .ivu-btn {
    margin-left: -5px;
    border-radius: 0 4px 4px 0;
  }
}
</style>
